<template>
	<view class="column-css" style="height: 100%;">
		<view class="column-css">
			<!-- <button style="margin: 0 150rpx;" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"><view>微信账号快捷登录</view></button> -->
			<view class="column-css" style="margin: 100rpx 0;margin-top: 150rpx; align-items: center;text-align: center;">
				<image style="width: 165rpx;height: 165rpx;border-radius: 50%;overflow:hidden;" src="../../static/icon_logo.png"></image>
				<view style="font-size: 28rpx;margin-top: 20rpx;">网络货运-司机</view>
			</view>
			
			<button v-show="ischeckbox" style="margin: 0 150rpx;" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
				<view class="row-css" style="justify-content: center;align-items: center;">
					<!-- <image style="width: 49rpx;height: 40rpx;padding-right: 10rpx;" mode="aspectFit" src="../../static/icon_wx.png"></image> -->
					手机号快捷登录
				</view>
			</button>
			<button v-show="!ischeckbox" style="margin: 0 150rpx;" type="primary" @click="getWX()">
				<view class="row-css" style="justify-content: center;align-items: center;">
					<!-- <image style="width: 49rpx;height: 40rpx;padding-right: 10rpx;" mode="aspectFit" src="../../static/icon_wx.png"></image> -->
					手机号快捷登录
				</view>
			</button>
			<view style="text-align: center;margin: 20rpx;color: #5F6579;" @click="onloging()">输入手机号码登录/注册</view>
		</view>
		<view class="position-pop" style="flex-direction: column; align-items: center;">
			<view style="color: #2B7CFF;font-size: 24rpx;margin: 40rpx auto;" @click="goBackAccount">
				如原手机号无法使用，请点击找回账号
			</view>
			<view class="row-css" style="margin-bottom: 20rpx; align-items: center;">
				<checkbox :checked="ischeckbox" style="transform:scale(0.7)" @click="checkbox()"></checkbox>
				<view class="row-css">
					<text style="color: #2C7DFF;" @click="onproctocl(1)">《服务协议》</text>
					与
					<text style="color: #2C7DFF;" @click="onproctocl(2)">《隐私政策》</text>
				</view>
			</view>
		<!-- <uni-popup ref="popup" type="bottom">
			<view class="column-css" style="background: #FFFFFF;border-top-left-radius: 15rpx;border-top-right-radius: 15rpx;">
				<view style="padding: 20rpx;">服务条款以及隐私政策</view>
				<view style="padding: 0 40rpx;">
					在您注册成为运易通会员的过程中，您需要通过点击同意的形式在线签署《运易通服务条款》、《运易通隐私政策》，请您务必仔细阅读、充分理解条款内容后再点击同意（尤其是以粗体并下划线标识的条款，因为这些条款可能会明确您应履行的义务或对您的权利有所限制）。
					请您注意：如果您不同意上述服务条款、隐私政策或其中任何约定，请您停止注册。如您阅读并点击同意即表示您已充分阅读、理解并接受其全部内容，并表明您也同意运易通可以依据以上隐私政策来处理您的个人信息。如您对以上服务条款、隐私政策有任何疑问，您可联系运易通客服。
				</view>
				<view class="line-veiw"></view>
				<view style="padding: 20rpx 40rpx;">
					点击同意表示您已阅读并同意
					<text style="color: #2C7DFF;">《运易通服务条款》</text>
					与
					<text style="color: #2C7DFF;">《运易通隐私政策》</text>
				</view>
				<view class="row-css" style="padding: 20rpx 40rpx;justify-content: space-between;text-align: center;">
					<view @click="close"><button class="buttom-view" size="mini" type="primary" plain="true">不同意</button></view>
					<view @click="close"><button class="buttom-view" size="mini" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">同意</button></view>
				</view>
			</view>
		</uni-popup> -->
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<PrivacyPop ref="privacyPop" @agree="wxAgree" @disagree="wxDisAgree"></PrivacyPop>
		<!-- #endif -->
	</view>
</template>

<script>
import PrivacyPop from '@/components/privacyPopup/privacyPopup.vue'
export default {
	components: {
		PrivacyPop
	},
	data() {
		return {
			openid: '',
			unionid: '',
			ischeckbox: false,
			wxPrivacy: false
		};
	},
	methods: {
		wxAgree() {
			this.wxPrivacy = true
			this.ischeckbox = true
		},
		wxDisAgree() {
			uni.navigateBack({
				delta: 1
			})
		},
		checkbox: function() {
			this.ischeckbox = !this.ischeckbox;
		},
		onproctocl(type){
			uni.navigateTo({
				url: '../personal/proctcol?type='+type
			});
		},
		onloging() {
			uni.navigateTo({
				url: `login`
			});
		},
		getWX(){
			if(!this.ischeckbox){
				uni.showToast({
					title: '请先同意服务条款与隐私协议！',
					icon: 'none'
				});
				return;
			}
		},
		getcode() {
			let that = this;
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					that.code = loginRes.code;
					that.wxRequest({
						url: `${that.API}/miniApp/auth/code2Session`,
						method: 'POST',
						data: {
							code: that.code,
						},
						success: res => {
							if (res.data.code == 0) {
								console.log("code  ",res.data);
								that.openid = res.data.data.openid;
								that.unionid = res.data.data.unionid;
							}
						}
					});
				}
			});
		},
		quick() {
			this.$refs.popup.open('bottom');
		},
		close() {
			this.$refs.popup.close();
		},
		getPhoneNumber: function(e) {
			if(!this.ischeckbox){
				uni.showToast({
					title: '请先同意服务条款与隐私协议！',
					icon: 'none'
				});
				return;
			}
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				if (this.openid == '') {
					this.getcode();
					let that = this;
					that.wxRequest({
						url: `${that.API}/miniApp/auth/decodePhone`,
						method: 'POST',
						data: {
							encryptedData:  e.detail.encryptedData,
							iv: e.detail.iv,
							openid: that.openid, //手机号与openid绑定
							// unionid: that.unionid,
							// userInfo: '',
							// wxtype: '1'
						},
						success: async res => {
							// console.log(res,'--------','判断')
							const {type} = await that.getUserType(res.data.data.phoneNumber);
							if (type === "0") {
								uni.navigateTo({
									url: '/pages/user/businessTypes',
									success(result) {
										result.eventChannel.emit('login', {
											tel: res.data.data.phoneNumber,
											openid: res.data.data.openid
										})
									}
								})
							} else if (type === "1") {
								that.wxRequest({
									url: `${that.API}/miniApp/auth/wxLogin`,
									method: 'POST',
									data: {
										mobile: res.data.data.phoneNumber,
										openid: res.data.data.openid
									},
									title: '登录中...',
									success: res => {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 1)
										uni.switchTab({
											url:'/pages/index/taskhome'
										});
									}
								});
							} else if (type === "2") {
								that.wxRequest({
									url: `${that.API}/miniApp/auth/wxLoginCaptain`,
									method: 'POST',
									data: {
										mobile: res.data.data.phoneNumber,
										openid: res.data.data.openid
									},
									title: '登录中...',
									success: res => {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 2)
										uni.redirectTo({
											url:'/pages/ship/user/task'
										})
									}
								});
							}
						}
					});
				}else{
					let that = this;
					that.wxRequest({
						url: `${that.API}/miniApp/auth/decodePhone`,
						method: 'POST',
						data: {
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							openid: that.openid, //手机号与openid绑定
							unionid: that.unionid,
							userInfo: '',
							wxtype: '1'
						},
						title: '加载中',
						success: async res => {
							const {type} = await that.getUserType(res.data.data.phoneNumber);
							if (type === "0") {
								uni.navigateTo({
									url: '/pages/user/businessTypes',
									success(result) {
										result.eventChannel.emit('login', {
											tel: res.data.data.phoneNumber,
											openid: res.data.data.openid,
											login: 'wechat'
										})
									}
								})
							} else if (type === "1") {
								that.wxRequest({
									url: `${that.API}/miniApp/auth/wxLogin`,
									method: 'POST',
									data: {
										mobile: res.data.data.phoneNumber,
										openid: res.data.data.openid
									},
									title: '登录中...',
									success: res => {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 1)
										uni.switchTab({
											url:'/pages/index/taskhome'
										});
									}
								});
							} else if (type === "2") {
								that.wxRequest({
									url: `${that.API}/miniApp/auth/wxLoginCaptain`,
									method: 'POST',
									data: {
										mobile: res.data.data.phoneNumber,
										openid: res.data.data.openid
									},
									title: '登录中...',
									success: res => {
										uni.setStorageSync(this.DATA_KEY.userInfo, res.data.data.userModel);
										uni.setStorageSync(this.DATA_KEY.token, res.data.data.token);
										if(res.data.data.userModel.etpId != '' && res.data.data.userModel.etpName != ''){
											uni.setStorageSync(this.DATA_KEY.etpId, res.data.data.userModel.etpId);
											uni.setStorageSync(this.DATA_KEY.etpName, res.data.data.userModel.etpName);
										}
										uni.setStorageSync('loginType', 2)
										uni.reLaunch({
											url:'/pages/ship/index/task'
										})
									}
								});
							}
						}
						
					});
				}
			} else if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
				uni.showToast({
					title: '您拒绝了授权',
					icon: 'none'
				})
			}	else if (e.detail.errMsg === 'getPhoneNumber:fail 用户未绑定手机，请先在微信客户端进行绑定后重试') {
				uni.showToast({
					title: '您的微信未绑定手机号',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: '请求错误，请稍后重试',
					icon: 'none'
				})
			}
		},
		getUserType(tel) {
			return new Promise((resolve, reject) => {
				let that = this
				that.wxRequest({
					url: `${that.API}/captainApp/customerType?tel=` + tel,
					method: 'GET',
					success: res => {
						resolve(res.data.data)
					}
				})
			})
		},
		goBackAccount() {
			// uni.navigateTo({
			// 	url: '/pages/account/backAccount'
			// })
			this.wxRequest({
				url: `${this.API}/driverService/getSysOption`,
				method: 'GET',
				data: {},
				success: res => {
					console.log(res)
					if(res.data.msg == 1){
						uni.navigateTo({
							url: '/pages/account/backAccount'
						})
					} else {
						uni.showModal({
							content: '如您的原手机号无法接收登陆短信，请联系平台管理员找回账号并修改手机号',
							showCancel: false,
							success(modalRes) {
								if (modalRes.confirm) {
								}
							}
						})
					}
				}
			})
		},
	},
	onLoad() {
		this.getcode();
	},
	onShow() {
		this.getcode();
	}
};
</script>

<style>
page {
	background: #ffffff;
	height: 100%;
}
.row-css {
	display: flex;
	flex-direction: row;
}

.column-css {
	display: flex;
	flex-direction: column;
}

.buttom-view {
	width: 200rpx;
	border-radius: 50rpx;
	font-size: 30rpx;
}

.line-veiw {
	height: 5rpx;
	width: 100%;
	margin-top: 50rpx;
	background: #f2f2f2;
}

.position-pop {
	display: flex;
	position: fixed;
	bottom: 20rpx;
	justify-content: center;
	width: 100%;
	font-size: 24rpx;
}
</style>
